package baseball.controller;

import baseball.domain.Course;
import baseball.domain.User;
import baseball.mapper.CourseMapper;
import baseball.mapper.UserMapper;
import baseball.support.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RequestMapping("member/course")
@Controller
public class MemberCourseController extends BaseController {

    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private UserMapper userMapper;

    /**
     * 我的课程
     *
     * @param session
     * @return
     */
    @RequestMapping("myList")
    private String myList(HttpSession session, Model model) {
        User user = (User) session.getAttribute("sessionUser");
        Course course = new Course();
        course.setTeacher(user.getUsername());
        List<Course> list = courseMapper.select(course);
        model.addAttribute("list", list);
        return "member/course/list";
    }

    /**
     * 全部列表
     *
     * @param model
     * @return
     */
    @RequestMapping("list")
    private String list(Model model) {
        List<Course> list = courseMapper.selectAll();
        model.addAttribute("list", list);
        return "member/course/list";
    }

    /**
     * 到添加页面
     *
     * @return
     */
    @GetMapping("add")
    public String add(Model model) {
        /*User user = new User();
        user.setType("jl");
        List<User> userList = userMapper.select(user);
        model.addAttribute("userList", userList);*/
        return "member/course/add";
    }

    /**
     * 添加课程
     *
     * @param course
     * @return
     */
    @PostMapping("add")
    public String add(Course course, HttpSession session) {
        User user = (User) session.getAttribute("sessionUser");
        course.setTeacher(user.getUsername());
        course.setAddtime(new Date());
        course.setStatus("待审核");
        courseMapper.insertSelective(course);
        return "redirect:list.do";
    }


    /**
     * 课程详情
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("detail/{id}")
    public String detail(@PathVariable Integer id, Model model) {
        Course course = courseMapper.selectByPrimaryKey(id);
        model.addAttribute(course);
        User user = new User();
        user.setType("jl");
        List<User> userList = userMapper.select(user);
        model.addAttribute("userList", userList);
        return "member/course/detail";
    }

    /**
     * 更新课程
     *
     * @param course
     * @return
     */
    @RequestMapping("update")
    public String update(Course course) {
        courseMapper.updateByPrimaryKeySelective(course);
        return "redirect:list.do";
    }

    /**
     * 删除课程
     *
     * @param id
     * @return
     */
    @RequestMapping("del/{id}")
    public String del(@PathVariable Integer id) {
        courseMapper.deleteByPrimaryKey(id);
        return refresh();
    }

    /**
     * 上传文件
     *
     * @return
     */
    @PostMapping("upload")
    public String upload(MultipartFile file, Integer id) {
        Course course = courseMapper.selectByPrimaryKey(id);
        String saveFile = saveFile(file);
        course.setAttachment(saveFile);
        courseMapper.updateByPrimaryKeySelective(course);
        return refresh();
    }


    @RequestMapping("bm")
    public String bm(Integer courseid, RedirectAttributes model, HttpSession session) {
        User user = (User) session.getAttribute("sessionUser");

        Course course = courseMapper.selectByPrimaryKey(courseid);
        course.setNumyes(course.getNumyes() + 1);
        courseMapper.updateByPrimaryKeySelective(course);
        courseMapper.insertOrder(user.getId(), course.getId());
        model.addAttribute("msg", "报名成功");
        return refresh();

    }
}
